/*
 * ADCHubConnection.java
 *
 * Created on 04 November 2006, 15:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.conn.hub;

import libjdc.dc.io.DCBufferedReader;
import hubsniffer.ConnectHubThread;
import hubsniffer.DCHub;
import libjdc.dc.client.User;
import hubsniffer.conn.DefaultConnection;
import libjdc.dc.client.HubStatus;
import libjdc.dc.log.Log;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.sql.SQLException;

/**
 *
 * @author root
 */
public class ADCHubConnection extends DefaultConnection{
    
    /** Creates a new instance of ADCHubConnection */
    public ADCHubConnection(DCHub hub,User me) {
        super(hub,me,"UTF-8");
    }

    public void disconnect() throws UnsupportedEncodingException, IOException {
    }

    protected void processHubMessage(String string) {
    }
    
    public void run() {
        this.getHub().getHubSession().start();
        try {
            Socket hubSocket = new Socket(this.getHub().getHost(), this.getHub().getPort());
            //hubSocket.setSoTimeout(SOCKET_TIMEOUT);

            out = new DataOutputStream(hubSocket.getOutputStream());
            in = new DCBufferedReader(new InputStreamReader(hubSocket
                    .getInputStream(),getCharsetEncode()));
            
            this.getHub().getHubSession().setIs_online(isConnected= true); // isto � para sair..
            this.getHub().getHubSession().setHubStatus(HubStatus.Online);
            
            this.sendRaw("HSUP ADBAS0\n");
            //in.sniff();
            if(in.readMessage('\n').startsWith("ISUP")){
                 if(in.readMessage('\n').startsWith("ISID"))
                     System.out.println("ISID");
                 if(in.readMessage('\n').startsWith("IINF"))
                     System.out.println("IINF");
            }
            //System.out.println(in.readLine());
            System.out.println("passou");
            //while (isConnected)
               // processHubMessage(in.readMessage());
            
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace(Log.err);
            }
            
        } catch (IOException e) {
            e.printStackTrace(Log.err);
            
            if(e.getMessage().indexOf("Connection timed out")>0)
                this.getHub().getHubSession().setHubStatus(HubStatus.Timeout);
            
            isConnected = false;
            try {
                Log.out.println("DISCONNECT!!");
                disconnect();
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace(Log.err);
            } catch (IOException ex) {
                ex.printStackTrace(Log.err);
            }
            try {
                this.getHub().getHubSession().exceptionMessage(e.toString());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace(Log.err);
            }
        }
        this.getHub().getHubSession().end();
    }
    
    public static void main(String []args) throws InterruptedException, ClassNotFoundException, SQLException, FileNotFoundException{
        Log.setOutPutStream(System.out);
        Log.setErrOutPutStream(System.err);
        
        DCHub hub = new DCHub(70,"dcdev.no-ip.org", 16591,1);
        User me = new User("[PT]sniffer","{HubSniffer Project} <++ V:0.674,M:A,H:1/0/0,S:3>", "DSL","email@com.c", 104219547999L);

        //me.setHost(Host.getHostIP());

        long delay = 20000L;
        //HubClient.addHubClient(0,"PtokaX","","","Pk=PtokaX");
        //HubClient.addHubClient(4,"Verlihub","","","verlihub");
        //HubClient.addHubClient(5,"YnHub","","","Pk=YnHub");
        Log.out.println("Bla");
        //ConnectHubThread conn = new ConnectHubThread(new HubConnection(hub,me),delay);
        //ConnectHubThread conn = new ConnectHubThread(new HubInfoConnection(hub,me),delay);
        ConnectHubThread conn = new ConnectHubThread(new ADCHubConnection(hub,me),delay);
        conn.start();
        Log.out.println("Bla2");
        conn.join();
        Log.out.println("Bla3");
        
        hub.getHubSession().print();
        /*
        try {
            new HubINFOWriter(db).updateHubInfo(hub);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace(Sniffer.err);
        } catch (SQLException ex) {
            ex.printStackTrace(Sniffer.err);
        }*/
        
    }
    
}
